package com.panfeng.xcloud.boss.provider.assets.security;

import com.google.common.collect.Sets;
import com.panfeng.xcloud.common.core.constants.GlobalConstant;
import com.panfeng.xcloud.common.core.thread.ThreadLocalMap;
import com.panfeng.xcloud.common.security.vo.SysSecurityUser;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

import java.security.Principal;
import java.util.Collection;
import java.util.Set;

/**
 *
 * 用户权限相关工具类
 *
 * @author xiaobo
 * @version 1.0
 * @since 2018-12-28
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Slf4j
public final class SecurityUtils {

	public static String getCurrentLoginName() {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (principal instanceof UserDetails) {
			return ((UserDetails) principal).getUsername();
		}
		if (principal instanceof Principal) {
			return ((Principal) principal).getName();
		}
		return String.valueOf(principal);
	}

	public static SysSecurityUser getCurrentSecurityUser(Principal principal) {
		SysSecurityUser sysSecurityUser = (SysSecurityUser) ThreadLocalMap.get(GlobalConstant.Sys.TOKEN_AUTH_DTO);
		log.info(">>> 当前线程上下文安全认证信息SysSecurityUser={} <<<",sysSecurityUser);
		if (null == sysSecurityUser){
			if (principal instanceof OAuth2Authentication) {
				Object principalTmp = ((OAuth2Authentication) principal).getPrincipal();
				if (principalTmp instanceof SysSecurityUser) {
					return (SysSecurityUser) principalTmp;
				} else if (principalTmp instanceof Principal) {
					return null;
				}else{
					return null;
				}
			} else if (principal instanceof SysSecurityUser) {
				return (SysSecurityUser) principal;
			} else {
				return null;
			}
		}else {
			return sysSecurityUser;
		}
	}

	public static SysSecurityUser getCurrentSecurityUser() {
		SysSecurityUser sysSecurityUser = (SysSecurityUser)ThreadLocalMap.get(GlobalConstant.Sys.TOKEN_AUTH_DTO);
		log.info(">>> 当前线程上下文安全认证信息SysSecurityUser={} <<<",sysSecurityUser);
		if (null == sysSecurityUser) {
			Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			if (principal instanceof OAuth2Authentication) {
				Object principalTmp = ((OAuth2Authentication) principal).getPrincipal();
				if (principalTmp instanceof SysSecurityUser) {
					return (SysSecurityUser) principalTmp;
				} else if (principalTmp instanceof Principal) {
					return null;
				}else {
					return null;
				}
			} else if (principal instanceof SysSecurityUser) {
				return (SysSecurityUser) principal;
			} else {
				return null;
			}
		}else{
			return sysSecurityUser;
		}
	}

	public static Set<String> getCurrentAuthoritys() {
		Set<String> path = Sets.newHashSet();
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
		for (final GrantedAuthority grantedAuthority : authorities) {
			String authority = grantedAuthority.getAuthority();
			if (StringUtils.isNotEmpty(authority)) {
				path.add(authority);
			}
		}
		return path;
	}
}
